#!/usr/bin/env python

import math
import sys

arq = []
num = []

for i in range(1,4):
  arq += [open("../logs/order" + str(i) + ".log")]
  num.append([])

for k in range(0,3):
  
  print "========= Arquivo " + str(k+1) + " ============="
  for line in arq[k]:
    x = line.split(" ")
    if float(x[0]) < 60.0 or float(x[0]) > 540.0:
      continue
    num[k].append(long(x[1]))
  print "Numero de mensagens: " + str(len(num[k])) + "\n"

  i = 0

  expected = num[k][0]+1
  changes = 0
  lost = 0

  while i < len(num[k])-1:
    i = i + 1

    if (num[k][i] < expected):
      print "Mensagem " + str(num[k][i]) + " fora de ordem esperava " + str(expected)
      changes += 1
    elif (num[k][i] > expected):
      # perdeu mensagem
      print "Perdeu mensagem, recebeu " + str(num[k][i]) + " esperava " + str(expected)
      lost = lost + (num[k][i] - expected)
      expected = num[k][i]+1
    else:
      expected = num[k][i]+1

  if(changes != 0):
    print "\nTrocas de mensagens: " + str(changes)
  else:
    print "\nTransporte " + str(k+1) + " totalmente ordenado"

  print "Perdeu " + str(lost) + " mensagens\n"

  arq[k].close()
